<?php
	session_start();	
?>

<?php
	if( $_SESSION["admin"] != "" )
	{
?>
<a href="index.php?com=template"><b>List template</b></a> | <a href="index.php?com=template&func=add"><b>Add template</b></a>
<?php
	include_once("process/template.php");	
	$template = new template();
	
	if( $_GET["com"] == "template" && $_GET["func"] == "update" && isset($_GET["id"]))
	{
		$row = $template->GetOneTemplate($_GET['id']);	
		$path = rtrim($_SERVER['PHP_SELF'],"index.php")."/library/fckeditor/";
	}elseif( isset($_POST['btnUpdate']) )
	{	
		$file = $_FILES["template_image"];
		$file_name = $file["name"];
		$file_ext = explode(".",$file_name);
		if($file["name"] == "")
		{			
			$description=stripcslashes($_POST["template_description"]);
			$path=stripcslashes($_POST["template_path"]);
            $name = stripcslashes($_POST["template_name"]);
            $id = stripcslashes($_POST["template_id"]);
            echo $id, $name, $path, $description;
			$template->UpdateTemplate($id, $name, $path, $description, $file["name"]);
			
			echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=template" />';
			return;
		}
		if($file["name"] != "" && ($file_ext[count($file_ext)-1] == "jpg" || $file_ext[count($file_ext)-1] == "jpeg" || $file_ext[count($file_ext)-1] == "gif"))
		{					
			$description=stripcslashes($_POST["template_description"]);
			$path=stripcslashes($_POST["template_path"]);
            $name = stripcslashes($_POST["template_name"]);
			$template->UpdateTemplate($name, $path, $description, $file["name"],$_POST["template_id"]);
			
			unlink("../asset/".$_POST['template_image']);
			move_uploaded_file($_FILES["template_image"]["tmp_name"],"../asset/".$_FILES["template_image"]["name"]);
			
			echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=template" />';
			return;
		}
		else if($file["name"] != "" && ($file_ext[count($file_ext)-1] != "jpg" && $file_ext[count($file_ext)-1] != "jpeg" && $file_ext[count($file_ext)-1] != "gif"))
		{
			$alert='<script type="text/javascript">
			alert("Bạn chỉ được phép chọn file có định dạng jpg, jpeg, gif");
			</script>';
			echo $alert;
		}
	}
	else
	{
		echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=template" />';
	}	
	
?>
<form action="index.php?com=template&func=update" id="contactform" method="post" enctype="multipart/form-data">
	<table width="630px" style="font-weight:bold; font-size:10pt;" border="0">
		<tr>
			<td align="center" colspan="4" height="50"><b style="font-size:12pt; color:Blue;">Update template</b></td>
		</tr>
		<tr height="26">
			<td align="right" width="120px">Template name :&nbsp;&nbsp;
				<input type="hidden" name="template_id" id="template_id" value="<?php echo $row['template_id'] ?>" />
			</td>
			<td width="140px"><input type="text" class="validate[required]" name="template_name" id="template_name" value="<?php echo $row['template_name']; ?>" style="width: 120px;" /><font style="color: red;"> *</font>
				<input type="hidden" name="template_id" id="template_id" value="<?php echo $row['template_id'] ?>" />
			</td>
        </tr>
        <tr>
			<td align="right" width="120px">Template path :&nbsp;&nbsp;</td>
			<td><input type="text" class="validate[required]" name="template_path" id="template_path" value="<?php echo $row['template_path']; ?>" style="width: 120px;" /><font style="color: red;"> *</font></td>
		</tr>  
        <tr>
			<td valign="top" style="padding-top:4px;" align="right">Template description :&nbsp;&nbsp;<br /></td>
			<td colspan="3" style="padding-top:4px;">
        	<?php
				include("library/fckeditor/fckeditor.php");
            	$summary = new FCKeditor("template_description");
				$summary->BasePath = $path;
				$summary->Height = 300;
				$summary->Value = $row["template_description"];
				$summary->Create();
			?>
			</td>
        </tr>    	    
        <tr>
			<td align="right">Template image :&nbsp;&nbsp;</td>
			<td>
				<input type="file" name="template_image" id="template_image" />
				<input type="hidden" name="template_image" id="template_image" value="<?php echo $row['template_image']; ?>" />
			</td>
		</tr>
		<tr>
			<td></td>
			<td colspan="3" height="40">
				<input type="submit" name="btnUpdate" id="btnUpdate" value="Update" />
				<input type="reset" value="Nhập lại" />
			</td>
		</tr>
    </table>
</form><br><br>
<i>Please fill in these fields (<font style="color: red;">*</font>)</i>
<?php
    }
   else
	{
		echo "<meta http-equiv='refresh' content='0; url=login.php' />";
	}
?>